@extends('layouts.app')

@section('title','新增收货地址')

@section('content')
<div class="row">
    <div class="col-md-10 offset-lg-1">
        <div class="card">
            <div class="card-header">
                <h2 class="text-center">
                    {{-- 通过主键值判断是增还是改 --}}
                    {{ $address->id ? '修改': '新增' }}收货地址
                </h2>
            </div>
            <div class="card-body">
                {{-- 输出后端报错 --}}
                @if (count($errors) > 0)
                <div class="alert alert-danger">
                    <h4>
                        有错误发生：
                    </h4>
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li><i class="glyphicon glyphicon-remove"></i> {{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
                @endif

                <!-- inline-template 代表通过内联方式引入组件 -->
                <user-addresses-create-and-edit inline-template>
                    @if ($address->id)
                        <form class="form-horizontal" role="form" action="{{ route('user_addresses.update', ['user_address' => $address->id]) }}" method="post">
                        {{ method_field('PUT') }}
                    @else
                        <form action="{{ route('user_addresses.store') }}" method="post" class="form-horizontal" role="form">
                    @endif
                        {{-- 引入csrf-token字段 --}}
                        {{ csrf_field() }}

                        {{-- 添加change函数，vue联动组件 --}}
                        {{-- php转json字符串数组入vue组件 --}}
                        <select-district :init-value="{{ json_encode([$address->province, $address->city, $address->district]) }}"  @change="onDistrictChanged" inline-template>
                            <div class="form-group row">
                                <label class="col-form-label col-sm-2 text-md-right">省市区</label>
                                <div class="col-sm-3">
                                    <select  class="form-control" v-model="provinceId">
                                        <option value="">选择省</option>
                                        {{-- 使用@赋值避免与blade模板冲突 --}}
                                        <option v-for="(name,id) in provinces" :key="id" :value="id">@{{ name }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-3">
                                    <select class="form-control" v-model="cityId">
                                        <option value="">选择市</option>
                                        <option v-for="(name, id) in cities" :value="id" :key="id">@{{ name }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-3">
                                    <select class="form-control" v-model="districtId">
                                        <option value="">选择区</option>
                                        <option v-for="(name, id) in districts" :value="id" :key="id">@{{ name }}</option>
                                    </select>
                                </div>
                            </div>
                        </select-district>

                        {{-- blade模板，v-model数据值"双向"与user-addresses-create-and-edit组件联动  --}}
                        <input type="hidden" name="province" v-model="province">
                        <input type="hidden" name="city" v-model="city">
                        <input type="hidden" name="district" v-model="district">
                        <div class="form-group row">
                            <label class="col-form-label text-md-right col-sm-2">详细地址</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="address" value="{{ old('address', $address->address) }}">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-form-label text-md-right col-sm-2">邮编</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="zip" value="{{ old('zip', $address->zip) }}">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-form-label text-md-right col-sm-2">姓名</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="contact_name" value="{{ old('contact_name', $address->contact_name) }}">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-form-label text-md-right col-sm-2">电话</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="contact_phone" value="{{ old('contact_phone', $address->contact_phone) }}">
                            </div>
                        </div>
                        <div class="form-group row text-center">
                            <div class="col-12">
                                <button type="submit" class="btn btn-primary">提交</button>
                            </div>
                        </div>
                    </form>
                    
                </user-addresses-create-and-edit>
    
            </div>
        </div>
        
    </div>
</div>
    
@endsection